using System.Collections.Generic;
using System.Xml;

namespace NGIS.Model
{
	public class ModelRuntime : IModelRuntime
	{
		private string mName;

		private string mVersion;

		private string mEntry;

		private string mBaseDirectory;

		private List<HardwareRequirement> mHardwareRequirementList = new List<HardwareRequirement>();

		private List<SoftwareRequirement> mSoftwareRequirementList = new List<SoftwareRequirement>();

		private List<ModelAssembly> mModelAssemblyList = new List<ModelAssembly>();

		private List<SupportiveResource> mSupportiveResourceList = new List<SupportiveResource>();

		public bool loadFromXml(XmlElement pModelClassElement)
		{
			XmlElement xmlElement = pModelClassElement.ChildNodes[2] as XmlElement;
			mName = xmlElement.GetAttribute("name");
			mVersion = xmlElement.GetAttribute("version");
			mBaseDirectory = xmlElement.GetAttribute("baseDir");
			mEntry = xmlElement.GetAttribute("entry");
			XmlElement xmlElement2 = xmlElement.ChildNodes[0] as XmlElement;
			XmlElement xmlElement3 = xmlElement.ChildNodes[1] as XmlElement;
			XmlElement xmlElement4 = xmlElement.ChildNodes[2] as XmlElement;
			XmlElement xmlElement5 = xmlElement.ChildNodes[3] as XmlElement;
			foreach (object childNode in xmlElement2.ChildNodes)
			{
				XmlElement xmlElement6 = childNode as XmlElement;
				HardwareRequirement item = default(HardwareRequirement);
				item.requirementKey = xmlElement6.GetAttribute("key");
				item.requirementValue = xmlElement6.GetAttribute("value");
				mHardwareRequirementList.Add(item);
			}
			foreach (object childNode2 in xmlElement3.ChildNodes)
			{
				XmlElement xmlElement7 = childNode2 as XmlElement;
				SoftwareRequirement item2 = default(SoftwareRequirement);
				item2.requirementKey = xmlElement7.GetAttribute("key");
				item2.requirementValue = xmlElement7.GetAttribute("value");
				item2.requirementPlatform = xmlElement7.GetAttribute("platform");
				mSoftwareRequirementList.Add(item2);
			}
			foreach (object childNode3 in xmlElement4.ChildNodes)
			{
				XmlElement xmlElement8 = childNode3 as XmlElement;
				ModelAssembly item3 = default(ModelAssembly);
				item3.assemblyName = xmlElement8.GetAttribute("name");
				item3.assemblyPath = xmlElement8.GetAttribute("path");
				mModelAssemblyList.Add(item3);
			}
			foreach (object childNode4 in xmlElement5.ChildNodes)
			{
				XmlElement xmlElement9 = childNode4 as XmlElement;
				SupportiveResource item4 = default(SupportiveResource);
				item4.resourceType = xmlElement9.GetAttribute("type");
				item4.resourceName = xmlElement9.GetAttribute("name");
				mSupportiveResourceList.Add(item4);
			}
			return true;
		}

		public bool formatToXml(XmlElement pModelClassElement)
		{
			XmlDocument ownerDocument = pModelClassElement.OwnerDocument;
			XmlElement xmlElement = ownerDocument.CreateElement("Runtime");
			xmlElement.SetAttribute("name", mName);
			xmlElement.SetAttribute("version", mVersion);
			xmlElement.SetAttribute("baseDir", mBaseDirectory);
			xmlElement.SetAttribute("entry", mEntry);
			XmlElement xmlElement2 = ownerDocument.CreateElement("HardwareConfigures");
			XmlElement xmlElement3 = ownerDocument.CreateElement("SoftwareConfigures");
			XmlElement xmlElement4 = ownerDocument.CreateElement("Assemblies");
			XmlElement xmlElement5 = ownerDocument.CreateElement("SupportiveResources");
			for (int i = 0; i < mHardwareRequirementList.Count; i++)
			{
				XmlElement xmlElement6 = ownerDocument.CreateElement("Add");
				xmlElement6.SetAttribute("key", mHardwareRequirementList[i].requirementKey);
				xmlElement6.SetAttribute("value", mHardwareRequirementList[i].requirementValue);
				xmlElement2.AppendChild(xmlElement6);
			}
			for (int j = 0; j < mSoftwareRequirementList.Count; j++)
			{
				XmlElement xmlElement7 = ownerDocument.CreateElement("Add");
				xmlElement7.SetAttribute("key", mSoftwareRequirementList[j].requirementKey);
				xmlElement7.SetAttribute("value", mSoftwareRequirementList[j].requirementValue);
				xmlElement7.SetAttribute("platform", mSoftwareRequirementList[j].requirementPlatform);
				xmlElement3.AppendChild(xmlElement7);
			}
			for (int k = 0; k < mModelAssemblyList.Count; k++)
			{
				XmlElement xmlElement8 = ownerDocument.CreateElement("Assembly");
				xmlElement8.SetAttribute("name", mModelAssemblyList[k].assemblyName);
				xmlElement8.SetAttribute("path", mModelAssemblyList[k].assemblyPath);
				xmlElement4.AppendChild(xmlElement8);
			}
			for (int l = 0; l < mSupportiveResourceList.Count; l++)
			{
				XmlElement xmlElement9 = ownerDocument.CreateElement("Add");
				xmlElement9.SetAttribute("type", mSupportiveResourceList[l].resourceType);
				xmlElement9.SetAttribute("name", mSupportiveResourceList[l].resourceName);
				xmlElement5.AppendChild(xmlElement9);
			}
			xmlElement.AppendChild(xmlElement2);
			xmlElement.AppendChild(xmlElement3);
			xmlElement.AppendChild(xmlElement4);
			xmlElement.AppendChild(xmlElement5);
			pModelClassElement.AppendChild(xmlElement);
			return true;
		}

		public string getName()
		{
			return mName;
		}

		public void setName(string pName)
		{
			mName = pName;
		}

		public string getVersion()
		{
			return mVersion;
		}

		public void setVersion(string pVersion)
		{
			mVersion = pVersion;
		}

		public string getEntry()
		{
			return mEntry;
		}

		public void setEntry(string pEntry)
		{
			mEntry = pEntry;
		}

		public string getBaseDirectory()
		{
			return mBaseDirectory;
		}

		public void setBaseDirectory(string pDirectory)
		{
			mBaseDirectory = pDirectory;
		}

		public bool addHardwareRequirement(ref HardwareRequirement pRequirement)
		{
			for (int i = 0; i < mHardwareRequirementList.Count; i++)
			{
				if (mHardwareRequirementList[i].requirementKey == pRequirement.requirementKey && mHardwareRequirementList[i].requirementValue == pRequirement.requirementValue)
				{
					return false;
				}
			}
			mHardwareRequirementList.Add(pRequirement);
			return true;
		}

		public bool addSoftwareRequirement(ref SoftwareRequirement pRequirement)
		{
			for (int i = 0; i < mSoftwareRequirementList.Count; i++)
			{
				if (mSoftwareRequirementList[i].requirementKey == pRequirement.requirementKey && mSoftwareRequirementList[i].requirementValue == pRequirement.requirementValue)
				{
					return false;
				}
			}
			mSoftwareRequirementList.Add(pRequirement);
			return true;
		}

		public bool addModelAssembly(ref ModelAssembly pRequirement)
		{
			for (int i = 0; i < mModelAssemblyList.Count; i++)
			{
				if (mModelAssemblyList[i].assemblyName == pRequirement.assemblyName && mModelAssemblyList[i].assemblyPath == pRequirement.assemblyPath)
				{
					return false;
				}
			}
			mModelAssemblyList.Add(pRequirement);
			return true;
		}

		public bool addSupportiveResource(ref SupportiveResource pRequirement)
		{
			for (int i = 0; i < mSupportiveResourceList.Count; i++)
			{
				if (mSupportiveResourceList[i].resourceType == pRequirement.resourceType && mSupportiveResourceList[i].resourceName == pRequirement.resourceName)
				{
					return false;
				}
			}
			mSupportiveResourceList.Add(pRequirement);
			return true;
		}

		public int getHardwareRequirementCount()
		{
			return mHardwareRequirementList.Count;
		}

		public int getSoftwareRequirementCount()
		{
			return mSoftwareRequirementList.Count;
		}

		public int getModelAssemblyCount()
		{
			return mModelAssemblyList.Count;
		}

		public int getSupportiveResourceCount()
		{
			return mSupportiveResourceList.Count;
		}

		public bool getHardwareRequirement(int idx, ref HardwareRequirement pRequirement)
		{
			if (idx < 0 || idx >= mHardwareRequirementList.Count)
			{
				return false;
			}
			pRequirement = mHardwareRequirementList[idx];
			return true;
		}

		public bool getSoftwareRequirement(int idx, ref SoftwareRequirement pRequirement)
		{
			if (idx < 0 || idx >= mSoftwareRequirementList.Count)
			{
				return false;
			}
			pRequirement = mSoftwareRequirementList[idx];
			return true;
		}

		public bool getModelAssembly(int idx, ref ModelAssembly pRequirement)
		{
			if (idx < 0 || idx >= mModelAssemblyList.Count)
			{
				return false;
			}
			pRequirement = mModelAssemblyList[idx];
			return true;
		}

		public bool getSupportiveResource(int idx, ref SupportiveResource pRequirement)
		{
			if (idx < 0 || idx >= mSupportiveResourceList.Count)
			{
				return false;
			}
			pRequirement = mSupportiveResourceList[idx];
			return true;
		}

		public bool removeHardwareRequirementref(HardwareRequirement pRequirement)
		{
			if (mHardwareRequirementList.Contains(pRequirement))
			{
				mHardwareRequirementList.Remove(pRequirement);
				return true;
			}
			return false;
		}

		public bool removeSoftwareRequirement(ref SoftwareRequirement pRequirement)
		{
			if (mSoftwareRequirementList.Contains(pRequirement))
			{
				mSoftwareRequirementList.Remove(pRequirement);
				return true;
			}
			return false;
		}

		public bool removeModelAssembly(ref ModelAssembly pRequirement)
		{
			if (mModelAssemblyList.Contains(pRequirement))
			{
				mModelAssemblyList.Remove(pRequirement);
				return true;
			}
			return false;
		}

		public bool removeSupportiveResource(ref SupportiveResource pRequirement)
		{
			if (mSupportiveResourceList.Contains(pRequirement))
			{
				mSupportiveResourceList.Remove(pRequirement);
				return true;
			}
			return false;
		}

		public bool removeHardwareRequirement(int idx)
		{
			if (idx < 0 || idx >= mHardwareRequirementList.Count)
			{
				return false;
			}
			mHardwareRequirementList.RemoveAt(idx);
			return true;
		}

		public bool removeSoftwareRequirement(int idx)
		{
			if (idx < 0 || idx >= mSoftwareRequirementList.Count)
			{
				return false;
			}
			mSoftwareRequirementList.RemoveAt(idx);
			return true;
		}

		public bool removeModelAssembly(int idx)
		{
			if (idx < 0 || idx >= mModelAssemblyList.Count)
			{
				return false;
			}
			mModelAssemblyList.RemoveAt(idx);
			return true;
		}

		public bool removeSupportiveResource(int idx)
		{
			if (idx < 0 || idx >= mSupportiveResourceList.Count)
			{
				return false;
			}
			mSupportiveResourceList.RemoveAt(idx);
			return true;
		}

		public bool compareOther(IModelRuntime pRuntime, ref string obj, ref string name)
		{
			if (mName != pRuntime.getName())
			{
				obj = "Runtime";
				name = "name";
				return false;
			}
			if (mVersion != pRuntime.getVersion())
			{
				obj = "Runtime";
				name = "version";
				return false;
			}
			if (mEntry != pRuntime.getEntry())
			{
				obj = "Runtime";
				name = "entry";
				return false;
			}
			if (mBaseDirectory != pRuntime.getBaseDirectory())
			{
				obj = "Runtime";
				name = "baseDir";
				return false;
			}
			if (mHardwareRequirementList.Count != pRuntime.getHardwareRequirementCount())
			{
				obj = "HardwareConfigures";
				name = "HardwareRequirementCount";
				return false;
			}
			for (int i = 0; i < mHardwareRequirementList.Count; i++)
			{
				HardwareRequirement hardwareRequirement = mHardwareRequirementList[i];
				HardwareRequirement pRequirement = default(HardwareRequirement);
				pRuntime.getHardwareRequirement(i, ref pRequirement);
				if (!hardwareRequirement.compareOther(pRequirement))
				{
					obj = "Hardware";
					name = hardwareRequirement.requirementKey + "<-->" + pRequirement.requirementKey;
					return false;
				}
			}
			if (mSoftwareRequirementList.Count != pRuntime.getSoftwareRequirementCount())
			{
				obj = "SoftwareConfigures";
				name = "SoftwareRequirementCount";
				return false;
			}
			for (int j = 0; j < mSoftwareRequirementList.Count; j++)
			{
				SoftwareRequirement softwareRequirement = mSoftwareRequirementList[j];
				SoftwareRequirement pRequirement2 = default(SoftwareRequirement);
				pRuntime.getSoftwareRequirement(j, ref pRequirement2);
				if (!softwareRequirement.compareOther(pRequirement2))
				{
					obj = "Software";
					name = softwareRequirement.requirementKey + "<-->" + pRequirement2.requirementKey;
					return false;
				}
			}
			if (mModelAssemblyList.Count != pRuntime.getModelAssemblyCount())
			{
				obj = "Assemblies";
				name = "AssemblyCount";
				return false;
			}
			for (int k = 0; k < mModelAssemblyList.Count; k++)
			{
				ModelAssembly modelAssembly = mModelAssemblyList[k];
				ModelAssembly pRequirement3 = default(ModelAssembly);
				pRuntime.getModelAssembly(k, ref pRequirement3);
				if (!modelAssembly.compareOther(pRequirement3))
				{
					obj = "Assembly";
					name = modelAssembly.assemblyName + "<-->" + pRequirement3.assemblyName;
					return false;
				}
			}
			if (mSupportiveResourceList.Count != pRuntime.getSupportiveResourceCount())
			{
				obj = "SupportiveResources";
				name = "SupportiveResourceCount";
				return false;
			}
			for (int l = 0; l < mSupportiveResourceList.Count; l++)
			{
				SupportiveResource supportiveResource = mSupportiveResourceList[l];
				SupportiveResource pRequirement4 = default(SupportiveResource);
				pRuntime.getSupportiveResource(l, ref pRequirement4);
				if (!supportiveResource.compareOther(pRequirement4))
				{
					obj = "SupportiveResource";
					name = supportiveResource.resourceName + "<-->" + pRequirement4.resourceName;
					return false;
				}
			}
			return true;
		}
	}
}
